package com.tianji.learning.mq;

import com.tianji.api.dto.trade.OrderBasicDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author ffk
 * @date 2024/6/20 19:50
 */

@Component
@Slf4j
@RequiredArgsConstructor
public class listenTradeAndAdd2LearningLesson {

    private final ILearningLessonService learningLessonService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.Queue.LEARNING_LESSON_PAY_QUEUE,durable = "true"),
            exchange = @Exchange(name= MqConstants.Exchange.ORDER_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.ORDER_PAY_KEY
    ))
    public void listenAndAddLearningLesson(OrderBasicDTO orderBasicDTO) {
            if(ObjectUtils.isEmpty(orderBasicDTO.getOrderId())
                    || ObjectUtils.isEmpty(orderBasicDTO.getUserId())
                    || ObjectUtils.isEmpty(orderBasicDTO.getCourseIds())
                    || ObjectUtils.isEmpty(orderBasicDTO.getFinishTime())
            )
            {
                log.error("免费课程购买后传到交换的信息有误，order信息为{}",orderBasicDTO);
                return ;
            }

            log.info("开始添加免费课程信息到课表中，调用tj-learning,订单信息为{}",orderBasicDTO);
            learningLessonService.addLessons(orderBasicDTO.getUserId(),orderBasicDTO.getCourseIds());


    }
}
